import Vue from 'vue'
import util from '../../util'
import axios from 'axios'
import { Toast, ActionSheet, Dialog } from 'vant';
import { BigNumber } from 'bignumber.js';
Vue.use(Toast, ActionSheet, Dialog);

export default {
    name: 'transfer',
    data () {
        return {
            amount: '',				//划转金额
            loading: false,			//按钮是否加载中

            defaultIcon: 'USDT',		//默认币种
            from: { id: 2, name: '' },
            to: { id: 1, name: '' },

            iconShown: false,		//是否显示币种选择弹窗
            coins: [],				//所有币种配置
            reverseList: [],			//永续支持的币种配置

            accountShown1: false,		//账户列表
            accountShown2: false,		//账户列表
            balance: {},					//总账户金额
            limitAmount: 0,				//返佣的时候最低限制
            only: 0,						//是否仅仅划转返佣
            isNigeria: false,           //是否尼日利亚版本
            autoWatch:false,			//defaultIcon是否有效
		}
	},
	watch:{
		amount(newVal){
			//最多小数点后保留2位
			if(newVal && newVal.indexOf('.') > -1){
				let arrNewVal = newVal.split('.');
				let _len = this.defaultIcon == 'USDT'?2:8;
				if(arrNewVal.length > 0 && arrNewVal[1].length > _len){
					arrNewVal[1] = arrNewVal[1].substr(0,_len);
					this.amount = arrNewVal.join('.');
				}
			}
		},
		allAmount(newVal,oldVal){
			//监听余额，有变动的时候或者余额非法，则置空输入框
			if(!newVal || isNaN(newVal) || newVal != oldVal) this.amount = '';
		},
		from(newVal,oldVal){
			//监听from字段，有变化的时候重新拉取余额
			if(newVal && oldVal && newVal.id != oldVal.id) this.getBalance()
		},
		defaultIcon(newVal){
			if(this.autoWatch){
				if(newVal != 'BTC' && newVal != 'USDT' && this.from.id === 3){
					this.from = {
						id:this.accountList1[0].id,
						name:this.accountList1[0].name
					}
					// this.from = { id:2, name: this.$t('transfer-key7') };		// 钱包账户
				}
				if(newVal != 'BTC' && this.from.id === 5 || newVal != 'USDT' && this.from.id === 4){
					this.from = {
						id:this.accountList1[0].id,
						name:this.accountList1[0].name
					}
				}
				if(newVal != 'BTC' && this.to.id === 5 || newVal != 'USDT' && this.to.id === 4){
					this.to = {
						id:this.accountList2[0].id,
						name:this.accountList2[0].name
					}
				}
				this.getBalance()
			}
			//避免选择DC
			// if(newVal === 'DC'){
			// 	this.defaultIcon = 'USDT';
			// }
			//非USDT不能选择正向合约
			if(newVal != 'USDT'){
				if(this.from.id === 7 || this.to.id === 7){
					this.from = this.accountConfig.find(item => item.id === 2);
					this.to = this.accountConfig.find(item => item.id === 1);
				}
			}
		}
	},
	computed:{
		iconList(){
			if(this.coins.length === 0) return [];
			let _arr = [];
			if(this.only && this.only === 3){
				//仅仅只有返佣
				if(this.balance && !this.balance.commission) return [];
				this.balance.commission.list.forEach(item => {
					_arr.push({
						name:item.symbol
					})
				})
			}else{
				//全部币种
				this.coins.map(item => {
					_arr.push({
						name:item.symbol
					})
				})
            }
            //尼日利亚只有USDT 
            if (this.isNigeria) {
                _arr = [{ name: 'USDT' }]
            }
			//高亮显示
			_arr.map(item => {
				item.color = this.defaultIcon==item.name?'#0080FF':'';
			})
			// return _arr.filter(item => item.name != 'DC');
			return _arr
		},
		standardList(){
			//计算币本位支持的币种列表
			if(this.balance && this.balance.reverse){
				let _arr = [];
				this.balance.reverse.list.forEach(item => {
					_arr.push(item.symbol)
				})
				return _arr
			}else{
				return []
			}
		},
		commission(){
			//返佣账户列表
			if(this.balance && this.balance.reverse){
				let _arr = [];
				this.balance.commission.list.forEach(item => {
					_arr.push(item.symbol)
				})
				return _arr
			}else{
				return []
			}
		},
		accountList1(){
			//from list
			let _arr = [];
			if(this.only === 3) return [];		//只划转返佣的时候
			if(this.isNi){
				//若尼日利亚版本
				// 钱包账户 只能转到闪电合约
				// 闪电合约账户  只能转到钱包账户
				// 返佣账户 能转到钱包账户、闪电合约账户
				// @Fishing 非洲的
				_arr = _arr = this.accountConfig.filter(item => item.id === 2 || item.id === 3 || item.id === 4)
			}else{
				this.accountConfig.forEach(item => {
					if(item.id != 3){
						_arr.push({
							id:item.id,
							name:item.name
						})
					}
				})
				//只有返佣账户支持的币种的时候，才能增加返佣
				if(this.commission.includes(this.defaultIcon)){
					_arr.push({
						id:3,
						name:this.$t('transfer-key6')
					})
				}
				//非BTC，则去掉币本位合约账户
				if(!this.standardList.includes(this.defaultIcon)){
					_arr.forEach((item,index) => {
						if(item.id === 5){
							_arr.splice(index,1)
						}
					})
				}
				//非USDT，则去掉差价合约账户 USDT合约
				if(this.defaultIcon != 'USDT'){
					_arr.forEach((item,index) => {
						if(item.id === 4){
							_arr.splice(index,1)
						}
					})
					_arr.forEach((item,index) => {
						if(item.id === 7){
							_arr.splice(index,1)
						}
					})
				}
			}
			//防止相同账户之间划转
			_arr.forEach((item,index) => {
				if(item.id === this.to.id){
					_arr.splice(index,1)
				}
			})

			_arr.map(item => {
				item.color = this.from.id==item.id?'#0080FF':'';
			})

			return _arr
		},
		accountList2(){
			//to list
			let _arr = [];
			if(this.isNi){
				_arr = _arr = this.accountConfig.filter(item => item.id === 2 || item.id === 4)
			}else{
				//肯定没返佣
				this.accountConfig.forEach(item => {
					if(item.id != 3){
						_arr.push({
							id:item.id,
							name:item.name
						})
					}
				})
				//非BTC，则去掉永续合约账户
				if(this.reverseList && this.reverseList.length){
					let _find = this.reverseList.find(item => item.InstrumentID.includes(this.defaultIcon));
					//若永续合约账户不支持当前币种，则去掉用户
					if(!_find) {
						_arr = _arr.filter(item => item.id != 5);
					}
				}
				
				//非USDT，则去掉差价合约账户  USDT合约
				if(this.defaultIcon != 'USDT'){
					_arr.forEach((item,index) => {
						if(item.id === 4){
							_arr.splice(index,1)
						}
					})
					_arr.forEach((item,index) => {
						if(item.id === 7){
							_arr.splice(index,1)
						}
					})
				}
			}

			//相同账户之间不能划转
			_arr.forEach((item,index) => {
				if(item.id === this.from.id){
					_arr.splice(index,1)
				}
			})
			
			//高亮显示
			_arr.map(item => {
				item.color = this.to.id==item.id?'#0080FF':'';
			})

			return _arr
		},
		accountConfig(){
			// 账户配置
			let accountList = [
                { id: 1, name: this.$t('transfer-key18') },		// 现货账户
                { id: 2, name: this.$t('transfer-key7') },		// 钱包账户
                { id: 3, name: this.$t('transfer-key6') },		// 返佣账户
                { id: 4, name: this.$t('transfer-key19') },		// 闪电合约账户
                { id: 5, name: this.$t('transfer-key20') },		// 永续合约账户
                { id: 7, name: this.$t('transfer-key37') },		// USDT合约
            ]
            if (this.isNigeria) accountList = accountList.filter(item => item.id != 1)
            return accountList
		},
		allAmount:{
			//总余额
			get:function(){
				if(Object.keys(this.balance).length == 0) return 0;
				let result = 0;
				if(this.from.id == 1){
					//现货账户
					let _filter = this.balance.stock.list.filter(item => item.symbol === this.defaultIcon);
					result = util.toNonExponential(_filter.length > 0?_filter[0].available:0);
				}
				if(this.from.id == 2){
					//钱包账户
					let _filter = this.balance.wallet.list.filter(item => item.symbol === this.defaultIcon);
					result = util.toNonExponential(_filter.length > 0?_filter[0].amount:0);
				}
				if(this.from.id == 3){
					//返佣账户
					let _filter = this.balance.commission.list.filter(item => item.symbol === this.defaultIcon);
					result = util.toNonExponential(_filter.length > 0?_filter[0].available:0);
				}
				if(this.from.id == 4){
					//差价合约账户
					let _filter = this.balance.contract.list.filter(item => item.symbol === this.defaultIcon);
					result = util.toNonExponential(_filter.length > 0?_filter[0].available:0);
				}
				if (this.from.id == 5) {
                    //永续合约账户
                    let _filter = this.balance.reverse.list.filter(item => item.symbol === this.defaultIcon);
                    result = util.toNonExponential(_filter.length > 0 ? _filter[0].withdrawable : 0);
                }
                if (this.from.id == 7) {
                    //USDT合约
                    let _filter = this.balance.forward.list.filter(item => item.symbol === this.defaultIcon);
                    result = util.toNonExponential(_filter.length > 0 ? _filter[0].withdrawable : 0);
                }
				// if(this.defaultIcon === 'USDT'){
				// 	result = util.countNum(result,2)
				// }
				//USDT切割成2位，其他币种切割8位
				result = util.countNum(result,this.defaultIcon === 'USDT'?2:8)
				return result
			},
			set:function(v){
				return v
			}
		},
	},
	methods:{
		change(){
			//to from 交换
			//特殊情况
			if(this.from.id == 3){
				Toast(this.$t('transfer-key35'))
				return
			}
			let temp = {
				id:this.to.id,
				name:this.to.name
			};
			this.to = {
				id:this.from.id,
				name:this.from.name
			}
			this.from = {
				id:temp.id,
				name:temp.name
			}
			// if(this.defaultIcon == 'USDT'){
			// 	if(this.from.id == 2){
			// 		//from是钱包账户
			// 		// this.to = {
			// 		// 	id:1,
			// 		// 	name:'现货账户'
			// 		// }
			// 		this.accountList1 = [];
			// 		this.accountList2 = [
			// 			{id:1,name:this.$t('transfer-key18')},
			// 			{id:4,name:this.$t('transfer-key19')}
			// 		]
			// 	}else if(this.to.id == 2){
			// 		this.accountList1 = [
			// 			{ id:1, name: this.$t('transfer-key18') },
			// 			{ id:4, name: this.$t('transfer-key19') },
			// 			{ id:3, name: this.$t('transfer-key6') },
			// 		]
			// 		this.accountList2 = [];
			// 	}
			// }
		},
		choiceIcon(){
            //弹窗显示币种选择
            if (this.isNigeria || this.isNi) return
			this.iconShown = true;
		},
		selectIcon(item){
			//选择币种
			this.defaultIcon = item.name;
			this.iconShown = false;
			// if(this.defaultIcon == 'USDT'){
			// 	this.from = {
			// 		id:2,name:this.$t('transfer-key7')
			// 	}
			// 	this.to = {
			// 		id:1,name: this.$t('transfer-key18')
			// 	}
			// 	this.accountList1 = [];
			// 	this.accountList2 = [
			// 		{id:1,name: this.$t('transfer-key18')},
			// 		{id:4,name: this.$t('transfer-key19')}
			// 	]
			// }else{
			// 	this.accountList1 = [
			// 		{ id:1, name: this.$t('transfer-key18') },
			// 		{ id:2, name: this.$t('transfer-key7') },
			// 	];
			// 	this.accountList2 = [
			// 		{ id:1, name: this.$t('transfer-key18') },
			// 		{ id:2, name: this.$t('transfer-key7') },
			// 	];
			// 	this.from = {id:2,name:this.$t('transfer-key7')};
			// 	this.to = {id:1,name: this.$t('transfer-key18')};
			// }
		},

        choiceAccount1 () {
            //from 选择
            if (this.accountList1.length === 0) return
            // this.accountList1.map(item => {
            // 	item.color = this.from.id==item.id?'#0080FF':'';
            // })
            this.accountShown1 = true;
        },
        selectAccount1 (item) {
            //选择弹窗中具体账户
            this.from = {
                id: item.id,
                name: item.name
            }
            this.accountShown1 = false;
        },

        choiceAccount2 () {
            //to 选择
            if (this.accountList2.length === 0) return
            // this.accountList2.map(item => {
            // 	item.color = this.to.id==item.id?'#0080FF':'';
            // })
            this.accountShown2 = true;
        },
        selectAccount2 (item) {
            //选择弹窗中具体账户
            this.to = {
                id: item.id,
                name: item.name
            }
            this.accountShown2 = false;
            //若选择返佣账户，则立马翻转
            // if(item.id == 3) this.change();
        },
        refreshNative () {
            util.callHandler('userBalanceChanged', {}, res => {
                console.log(res)
            })
        },
		checkoutTransfer (from_id, greenPass) {
            //当从合约划转出的时候，检测是否有弹窗等情况，其他情况直接过
            return new Promise((resolve, reject) => {
                // if (from_id != 4 && from_id != 5 || greenPass && greenPass == 1 || this.locale != 'zh') {
                if (from_id != 4 && from_id != 5 || greenPass && greenPass == 1) {
                    //绿色通道，或者非合约划出时直接通过
                    resolve()
                } else {
                    util.fetch(`${util.ajaxUrl}/user/new-balance`, {
                        token: this.token || util.getToken(),
                    }).then(xhr => {
                        //体验金余额
                        let tiyanjin = xhr.retData.contract.list.find(item => item.id === 3);
                        let ex_balance_e2 = tiyanjin?Number(tiyanjin.available):0;
                        let yongxuReverse = xhr.retData.reverse.list.find(item => Number(item.reverse) > 0 && item.symbol === this.defaultIcon);
                        if(yongxuReverse && from_id == 5){
                            //若有永续体验金，则提示
                            Dialog.confirm({
                                title: this.$t('transfer-key22'),
                                message: this.$t('transfer-key30',{amount:Number(yongxuReverse.reverse)}),
                                className: 'fullDialog2',
                                confirmButtonColor: '#1989fa',
                                confirmButtonText: this.$t('confirm'),
                                cancelButtonText: this.$t('cancel'),
                                overlayClass: 'sellType-mark'
                            }).then(() => {
                                this.upload(1)
                            }).catch(() => {
                                this.loading = false;
                            })
                        }else{
                            util.fetch(`${util.ajaxUrl}/user/order-list`, {
                                token: this.token || util.getToken(),
                            }).then(res => {
                                //判断是否有体验金持仓
                                let _filter = res.retData.list.filter(item => item.coupon_type == 4);
                                if (!ex_balance_e2) {
                                    resolve();
                                    return
                                }
                                if (ex_balance_e2 == 0 && _filter.length == 0) {
                                    //无体验金，无持仓
                                    resolve()
                                } else if (ex_balance_e2 > 0 && _filter.length == 0) {
                                    //有体验金，无持仓(纯提示，仍然可以划转)
                                    Dialog.confirm({
                                        title: this.$t('transfer-key22'),
                                        message: this.$t('transfer-key23') + '<br />' + this.$t('transfer-key24'),
                                        className: 'fullDialog2',
                                        confirmButtonColor: '#1989fa',
                                        confirmButtonText: this.$t('confirm'),
                                        cancelButtonText: this.$t('cancel'),
                                        overlayClass: 'sellType-mark'
                                    }).then(() => {
                                        this.upload(1)
                                    }).catch(() => {
                                        this.loading = false;
                                    })
                                } else if (ex_balance_e2 == 0 && _filter.length > 0 || ex_balance_e2 > 0 && _filter.length > 0) {
                                    //无体验金，有持仓    //有体验金，有持仓   (提示，并且不可划转)
                                    Toast(this.$t('transfer-key25'));
                                    this.loading = false;
                                } else if (ex_balance_e2 < 0) {
                                    Toast(this.$t('transfer-key26'))
                                    this.loading = false;
                                }
                            }).catch(error => {

                            })
                        }
                    }).catch(error => {

                    })

                }
            })
        },
        postMsg(){
        	if(this.to.id === 1 || this.to.id === 4 || this.to.id === 5 || this.to.id === 7) {
        		let msg = '';
        		if(this.to.id === 1) msg = this.$t('transfer-key31');
        		if(this.to.id === 4) msg = this.$t('transfer-key32');
        		if(this.to.id === 5) msg = this.$t('transfer-key33');
        		if(this.to.id === 7) msg = this.$t('transfer-key38');
	        	Dialog.confirm({
					title: '',
					message: msg,
					className:'fullDialog3',
					cancelButtonColor:'#1989fa',
					confirmButtonText:this.$t('transfer-key34'),
					cancelButtonText:this.$t('cancel'),
					overlayClass:'sellType-mark',
					confirmButtonColor:'rgb(25, 137, 250)',
					cancelButtonColor:'#454545',
					allowHtml:true
				}).then(() => {
					if(this.version >= 412){
						let _url = '';
						if(this.to.id == 1){
							//现货
							_url = 'flutterApp://switchIndexPage?type=SPOT&index=1';
						}else if(this.to.id == 4){
							//差价合约
							_url = 'flutterApp://switchIndexPage?type=CFD&index=2';
						}else if(this.to.id == 5){
							//永续
							_url = 'flutterApp://switchIndexPage?type=inverse&index=2';
						}else if(this.to.id == 7){
							//正向
							_url = 'flutterApp://switchIndexPage?type=USDT&index=2';
						}else{
							Toast(this.locale==='zh'?'错误':'error');
							return
						}
						util.callHandler('openUrl',{
							url:_url,
							title:'',
							needLogin:true,
							popVC:true,		//跳转的时候是否干掉当前页面
							hiddenNav:true,	//是否全屏
						},res => {
							console.log(res)
						})
					}else{
						if(this.to.id == 1){
							//现货
							util.callHandler('openUrl',{
								url: 'bbjApp://mainSpot',
								title:'',
								needLogin:false,
								popVC:false,		//跳转的时候是否干掉当前页面
				                hiddenNav:false,	//是否全屏
							},res => {
								console.log(res)
							})
						}else if(this.to.id == 4){
							//差价合约账户
							util.callHandler('openUrl',{
								url: 'bbjApp://tradeHome',
								title:'',
								needLogin:false,
								popVC:false,		//跳转的时候是否干掉当前页面
				                hiddenNav:false,	//是否全屏
				                type: 'CFD'
							},res => {
								console.log(res)
							})
						}else if(this.to.id == 5){
							//永续合约账户
							util.callHandler('openUrl',{
								url: 'bbjApp://tradeHome',
								title:'',
								needLogin:false,
								popVC:false,		//跳转的时候是否干掉当前页面
				                hiddenNav:false,	//是否全屏
				                type: 'inverse'
							},res => {
								console.log(res)
							})
						}else if(this.to.id == 7){
							//USDT合约账户(正向)
							util.callHandler('openUrl',{
								url: 'bbjApp://tradeHome',
								title:'',
								needLogin:false,
								popVC:false,		//跳转的时候是否干掉当前页面
				                hiddenNav:false,	//是否全屏
				                type: 'USDT'
							},res => {
								console.log(res)
							})
						}
					}
					console.log('confirm')
				}).catch(() => {
					console.log('cancel')
				})
			}
        },
		
		upload(greenPass){
			if(!this.amount || isNaN(this.amount) || this.amount == '0' || Number(this.amount) <= 0){
				return
			}
			if(Number(this.amount) > Number(this.allAmount)){
				// Toast(`可用余额仅为${this.allAmount}${this.defaultIcon}`);
				Toast(this.$t('transfer-key27',{amount:this.allAmount,name:this.defaultIcon}))
				return
			}
			this.loading = true;
			// if(){
			// 	//只有从返佣划转到钱包，且币种是USDT的时候才走这个接口
				
			// 	util.fetch(`${util.ajaxUrl}/transfer/partner`,{
			// 		token:this.token || util.getToken(),
			// 		amount:this.amount
			// 	}).then(res => {
			// 		this.loading = false;
			// 		this.amount = '';
			// 		if(res.retCode === 0){
			// 			this.refreshNative();
			// 			Toast(res.retMsg || this.$t('success'));
			// 			this.getBalance();
			// 		}else{
			// 			Toast(res.retMsg || this.$t('noError'))
			// 		}
			// 	}).catch(error => {
			// 		this.loading = false;
			// 		this.amount = '';
			// 		console.log(error)
			// 	})
			// }else{
				if(this.from.id == 3 && this.defaultIcon != 'USDT'){
					//适配后台接口
					this.from.id = 6;
				}
				if(this.from.id == 3 && this.to.id == 2 && this.defaultIcon === 'USDT'){
					//返佣
					if(Number(this.amount) < Number(this.limitAmount)){
						// Toast(`返佣划转金额不低于${this.limitAmount}USDT`);
						Toast(this.$t('transfer-key14',{amount:this.limitAmount}))
						this.loading = false;
						return
					}
				}
				let amount = this.amount;
				this.checkoutTransfer(this.from.id,greenPass).then(() => {
					util.fetch(`${util.ajaxUrl}/transfer/transfer`,{
						token:this.token || util.getToken(),
						amount:amount,
						from_id:this.from.id,
						to_id:this.to.id,
						currency_id:this.defaultIcon
					}).then(res => {
						this.loading = false;
						this.amount = '';
						this.refreshNative();
						Toast(res.retMsg || this.$t('success'));
						if(res.retCode === 0) {
							this.getBalance();
							//划转成功后做个提示
							this.postMsg();
						}
						if(res.retCode == 88){
							//未开通的情况
							Dialog.confirm({
								title: this.$t('noAccountTitle'),
								message: this.$t('noAccountDesc'),
								className:'fullDialog2',
								cancelButtonColor:'#666',
								confirmButtonColor:'#1989fa',
								cancelButtonText:this.$t('cancel'),
								confirmButtonText:this.$t('confirm'),
								overlayClass:'sellType-mark'
							}).then(() => {
								util.callHandler('openUrl',{
									url:'bbjApp://tradeHome',
									title:'',
									needLogin:true,
									popVC:true,		//跳转的时候是否干掉当前页面
									hiddenNav:true,	//是否全屏
									type:'inverse'
								},res => {
									console.log(res)
								})
								// console.log('confirm')
							}).catch(() => {
								// console.log('cancel')
							})
						}
					}).catch(error => {
						this.loading = false;
						this.amount = '';
						console.log(error)
					}).finally(() => {
						//接口字段恢复
						if(this.from.id === 6) this.from.id = 3;
					})
				}).catch(e => {

				})
            // }
        },

        getAccountList () {
            //获取反向币种配置
            util.fetch2(`${util.ajaxUrl4}/query/v1.0/contract`, {
                pageSize: 20,
                pageIndex: 1,
                ExchangeID: 'DeepCoin'
            }).then(res => {
                this.reverseList = res.data;
                // console.log(this.reverseList)
            }).catch(error => {
                console.log(error)
            })
        },
        getCoinsList () {
            //拉取币种配置
            util.fetch(`${util.ajaxUrl}/site/spot-currency-list`, {
                app_id: 1
            }).then(res => {
                if (res.retCode === 0) {
                    this.coins = res.retData;
                } else {
                    Toast(res.retMsg || this.$t('noError'))
                }
            }).catch(error => {
                console.log(error)
            })
        },
        getBalance () {
            //获取余额
            this.allAmount = '';
            util.fetch(`${util.ajaxUrl}/user/new-balance`, {
                token: this.token || util.getToken(),
            }).then(res => {
                if (res.retCode === 0) {
                    this.balance = res.retData;
                } else {
                    Toast(res.retMsg || this.$t('noError'))
                }
            }).catch(error => {
                console.log(error)
            })
            //读取返佣最小划转金额
            util.siteConfig(this.token || util.getToken()).then(res => {
                this.limitAmount = res.min_rebate_amount;
            })
        },
        goHistory () {
            this.$router.push({
                path: '/history',
                query: {
                    title: this.$t('transfer-key1'),
                    name: this.defaultIcon,
                    isNigeria: this.isNigeria ? 1 : 0
                }
            })
        },
        amountBlur () { window.scrollTo(0, 0); },
        sellAll () { this.amount = this.allAmount; },
        goBack () { util.navigateBack(-1, this) },
        jump () {
            if (this.isNigeria) {
                this.$router.push({
                    path: '/nigeria/index',
                })
            } else {
            	if(this.version >= 430){
            		//跳转到flutter页面
            		util.callHandler('openUrl',{
						url: 'flutterApp://otcMainPage?selectTypeIndex=1',
						title:'',
						needLogin:false,
						popVC:false,		//跳转的时候是否干掉当前页面
		                hiddenNav:false,	//是否全屏
					},res => {
						console.log(res)
					})
            	}else{
	                let _query = this.defaultIcon === 'BTC' ? { BTC: 1 } : {};
	                this.$router.push({
	                    path: '/',
	                    query: _query
	                })
                }
            }
        },
        stop () { }
    },
    created () {
        if (this.$route.query.isNigeria) {
            this.isNigeria = true
        }
        this.from = { id: 2, name: this.$t('transfer-key7') };
        if (this.locale === 'zh') {
            this.to = { id: 1, name: this.$t('transfer-key18') };
        } else {
            this.to = { id: 4, name: this.$t('transfer-key19') };
        }
        if (this.isNigeria) {
            this.to = { id: 4, name: this.$t('transfer-key19') };
            localStorage.setItem('lang', 'en')
        }
        // if(this.$route.query.to && this.$route.query.to==4){
        // 	this.to = {
        // 		id:4,
        // 		name: this.$t('transfer-key19')
        // 	}
        // }
        if (this.$route.query.name) {
            //默认币种
            this.defaultIcon = this.$route.query.name;
        }
        if (this.$route.query.from) {
            //可能的from
            let _id = Number(this.$route.query.from);
            let _filter = this.accountConfig.filter(item => item.id === _id);
            if (_filter.length) {
                this.from = {
                    id: _id,
                    name: _filter[0].name
                }
            }
        }
        if (this.$route.query.to) {
            //可能的to
            let _id = Number(this.$route.query.to);
            let _filter = this.accountConfig.filter(item => item.id === _id);
            if (_filter.length) {
                this.to = {
                    id: _id,
                    name: _filter[0].name
                }
            }
        }
        if (this.$route.query.only) {
            //仅仅划转返佣
            this.only = Number(this.$route.query.only);
            this.from = { id: 3, name: this.$t('transfer-key6') };
            this.to = { id: 2, name: this.$t('transfer-key7') };
        }
        // this.accountList2 = [
        // 	{ id:1, name: this.$t('transfer-key18') },
        // 	{ id:3, name: this.$t('transfer-key6') },
        // 	{ id:4, name: this.$t('transfer-key19') },
        // ]
        setTimeout(()=>{
			this.autoWatch = true;
		},500)
        util.waitSDKReady().then(token => {
            this.token = token;
            this.getBalance();
            if (!this.isNigeria) {
                this.getCoinsList();
            }
            this.getAccountList();
        }).catch((e) => {
            Toast(this.$t('getTokenFail'))
        })
        setTimeout(() => {
            util.registerHandler('refreshCommonRequestWhenGoBackView', res => {
                if (this.token) this.getBalance();
                util.callHandler('setStatusBarStyle', {
                    style: 'light'
                })
            })
            //退出登录的时候native调用H5方法清除token缓存
            util.registerHandler('loginOutDeleteToken', res => {
                window.token = '';
                this.token = '';
                localStorage.removeItem('token');
            })
            util.callHandler('setStatusBarStyle', {
                style: 'light'
            })
        }, 500)
    }
}
